<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue3-escook</title>
  <link rel="stylesheet" href="./lib/style.css">
  <script type="importmap">
    {
      "imports" : {
        "vue": "./lib/vue-3.4.29.js"
      }
    }
  </script>
  <script type="module">
    import { createApp, ref, watch } from 'vue'

    const app = createApp({
      setup() {
        const user = ref({
          name: 'zs',
          address: {
            province: '河北省',
            city: '邯郸市'
          }
        })

        // 如果侦听的是响应式数据下某个属性，且这个属性的值是复杂类型的对象，
        // 则只能侦听到对象下属性的赋值操作，无法侦听到对象的替换操作
        // watch(user.value.address, (newObj, oldObj) => {
        //   console.log('侦听到了 user.value.address 的变化')
        //   console.log(newObj, oldObj)
        // })

        watch(() => user.value.address, (newObj, oldObj) => {
          console.log('侦听到了 user.value.address 的变化')
          console.log(newObj, oldObj)
        }, {
          // 开启深度侦听
          deep: true
        })

        const setValue = () => {
          // user.value.address.city = '沧州市'
          user.value.address = { a: 1, b: 2 }
        }

        return {
          user,
          setValue
        }
      }
    })

    app.mount('#app')
  </script>
</head>

<body>
  <div id="app">
    <button @click="setValue">赋值</button>
    <pre>{{ JSON.stringify(user, null, '    ') }}</pre>
  </div>
</body>

</html>